<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Checkin24 model
 *
 * Model used to retrive information from Checkin24
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Checkin24_model extends Base_model {
	function Checkin24_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";
		
		//needed for checkin24 support
		//$this->load->library("Nusoap_lib");

		log_message( 'debug', 'conlabz CMS : Checkin24_model class loaded' );
	}

	/**
	 * Get Checkin24 
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getCheckin24( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'checkin24.id, 
							checkin24.title,
							checkin24.objects' );
		$this->db->from( "checkin24" );
		$this->db->where( "website_id", $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('checkin24.title desc'); 
		return $this->db->get( );
	}

	/**
	 * Get Checkin24 
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getObjects( $checkin24_id, $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'checkin24.objects,
							checkin24.title' );
		$this->db->from( "checkin24" );
		$this->db->where( "checkin24.id", $checkin24_id );
		$this->db->where( "website_id", $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$objects = $this->db->get( );
		$row = $objects->result_array( );
		return unserialize( $row[0]['objects'] );
	}
	
	function insertObject( $checkin24_id, $object ){
		$new = array( '0' => array( $object['id'], $object['type'] ) );
		$objects = $this->getObjects( $checkin24_id );
		$result['objects'] = serialize( array_merge( $objects, $new ) );
		$this->db->update( 'checkin24', $result, array( 'id' => $checkin24_id, 'website_id' => $this->session->userdata( 'website_id' ) ) );
	}
	
	function getObjectView( $checkin24_id, $sort_type = '', $sort_order = '' ){
		$this->db->select( 'checkin24.objects,
							checkin24.title' );
		$this->db->from( "checkin24" );
		$this->db->where( "id", $checkin24_id );
		$objects = $this->db->get( );
		$row = $objects->result_array( );
		$row_array = unserialize( $row[0]['objects'] );
	
		foreach( $row_array as $key => $value ){
			$array[] = $value[0];
			$infos[$value[0]] = $value[1];
		}
		$return['h1'] = $row[0]['title'];
		
		//call checkin24.eu webservice
		$this->nusoap_client = new nusoap_client( "http://www.checkin24.eu/wsdl/index.php");
		 
		if( $this->nusoap_client->fault ) {
			$text = 'Error: '.$this->nusoap_client->fault;
		} else {
		        if( $this->nusoap_client->getError( ) ) {
				$text = 'Error: '.$this->nusoap_client->getError();
			} else {
				$row = $this->nusoap_client->call(
				                    'getObject',
						    		array( 'id' => $array ) );
				
				if( strlen( $row ) > 0 ) {
					$text = $row;
				} else {
					$text = "Empty";
				} //else
			} //else
		} //else
		
		$object = $this->xmltoarray->xmlarray( $text, 1, '', true );
		
		if( !empty( $object['object'] ) ){
			foreach( $object['object']['check'] as $key => $value ){
				foreach( $value as $k => $v ){
					$return['object'][$key][$k] = $v['value'];						
					if( $infos[$return['object'][$key]['id']] == 'information' ){
						$return['object'][$key]['information'] = 1;
					}
				}
			}
		}
		
		if( !empty( $sort_type ) ) {
			$sortArray = array();
		    foreach($return['object'] as $key => $array) {
		        $sortArray[$key] = $array[$sort_type];
		    }
		
		    array_multisort($sortArray, $sort_order, SORT_NUMERIC, $return['object']);
		}
		
		if( empty( $sort_type ) && empty( $sort_order ) ) {
	    	shuffle( $return['object'] );
		}
		
		return $return;
	}
	
	/**
	 * Constructs an array of the given checkin24 objects to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildDropdown( ){
		$checkin24 = $this->getCheckin24( );
		
		$dropdown = array( );
		foreach( $checkin24->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->title;
		}
		
		return $dropdown;
	}
	
	/**
	 * Delete Objects
	 *
	 * @access public
	 * @param $object_id, $object_category_id
	 */
	function delete_object( $id, $checkin24_id ) {		
		$result = array( );
		$objects = $this->getObjects( $checkin24_id );
		foreach( $objects as $key => $value ){
			if( $value[0] != $id ){
				$result[] = $value;
			}
		}

		$objects = serialize( $result );
		
		$this->db->where( array( 'website_id' => $this->session->userdata( 'website_id' ),
								 'id' => $checkin24_id ) );
		$this->db->update( 'checkin24', array( 'objects' => $objects ) );
	}
}
/* End of file blog_model.php */
/* Location: ./modules/blog/models/blog_model.php */